using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：（新产品开发）高温放置寿命10PCS
    /// </summary>
    public class R028 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            BaseReport.ErrorMsg = "";

            #region 表头
            int headColumnsIndex = 0;

            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //专案编号
                    sheet.GetRow(2).GetCell(3).SetCellValue(head.ProNo);
                    //专案名称
                    sheet.GetRow(2).GetCell(7).SetCellValue(head.ProName);

                    sheet.GetRow(3).GetCell(3).SetCellValue(head.LotNo);
                    
                    //设计寿命
                    sheet.GetRow(3).GetCell(7).SetCellValue(head.TestTotalTime);
                    //尺寸
                    sheet.GetRow(3).GetCell(11).SetCellValue(head.Size);
                    sheet.GetRow(3).GetCell(14).SetCellValue(head.RippleCurrent);
                    sheet.GetRow(3).GetCell(17).SetCellValue(head.TestFreq);
                    sheet.GetRow(4).GetCell(3).SetCellValue(head.TestVoltage);
                    sheet.GetRow(4).GetCell(7).SetCellValue(head.TestTemp);
                    sheet.GetRow(4).GetCell(11).SetCellValue(head.Series);
                    sheet.GetRow(4).GetCell(14).SetCellValue(head.Cap);
                    sheet.GetRow(4).GetCell(17).SetCellValue(head.Voltage);
                    
                    //材料料号
                    sheet.GetRow(3).GetCell(20).SetCellValue(head.FoilP);
                    sheet.GetRow(3).GetCell(24).SetCellValue(head.FoilN);
                    sheet.GetRow(3).GetCell(26).SetCellValue(head.Eps);
                    sheet.GetRow(3).GetCell(30).SetCellValue(head.LeadWire);
                    sheet.GetRow(3).GetCell(34).SetCellValue(head.Rubber);
                    sheet.GetRow(3).GetCell(36).SetCellValue(head.AlCase);
                    sheet.GetRow(3).GetCell(39).SetCellValue(head.Paste);
                    
                    //材料厂商
                    sheet.GetRow(4).GetCell(20).SetCellValue(head.FoilpSupplier);
                    
                    sheet.GetRow(85).GetCell(2).SetCellValue(head.ApproveUser);
                    sheet.GetRow(85).GetCell(27).SetCellValue(head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                    BaseReport.ErrorMsg += (Environment.NewLine + ex.Message);
                }
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 9;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 6;
            int dtlColumns = 8;
            int dtlRowsCount = 17;

            string RT = "RT:";
            string RH = "RH:";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i >= 10) break;

                try
                {
                    if (i == 0)
                    {
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                                                head.Dtls[i].TestTime.ToString() + "hrs");
                    }
                    else
                    {
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex+1).SetCellValue(
                            head.Dtls[i].TestTime.ToString() + "hrs");
                    }
                    
                    sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 4).SetCellValue(
                            head.Dtls[i].TestDate.ToString("yyyy/M/d"));
                    
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 1).SetCellValue(
                        head.Dtls[i].GetTemp());
                    
                    sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 4).SetCellValue(
                        head.Dtls[i].GetHumidity());

                    sheet.GetRow(currentHeadRowIndex + 16).GetCell(currentDtlColumnIndex).SetCellValue(
                        head.Dtls[i].Remark);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                    BaseReport.ErrorMsg += (Environment.NewLine + ex.Message);
                }

                #region data
                try
                {
                    int cIndex = currentDataColumnIndex;
                    var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                    if (caps.Any())
                    {
                        var carpss = caps.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(dataRowIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = carpss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                        //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                        for (int k = 0; k < carpss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (carpss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    if (i == 0) cIndex++;
                    else cIndex += 2;
                    var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                    if (dfs.Any())
                    {
                        var dfss = dfs.ToList();
                        for (int k = 0; k < dfss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (dfss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value, 2));
                        }
                    }

                    cIndex++;
                    var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    lcs = null;
                    cIndex++;
                    lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "ESR");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    lcs = null;
                    cIndex++;
                    lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    lcs = null;
                    cIndex++;
                    lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "L");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                }
                catch(Exception ex) {
                    Logger.LogError(ex);
                    BaseReport.ErrorMsg += (Environment.NewLine + ex.Message);
                }
                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;

                if (i==4)
                {
                    currentDtlColumnIndex = 1;
                    currentDataColumnIndex = 1;
                    currentHeadRowIndex = 23;
                    dataRowIndex = 26;
                }
            }

            #endregion

            //int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            //int firstDataRow = 5;
            //if (headCount < 5)
            //{
            //    int lastDataRowIndex = firstDataRow + dtlRowsCount * 5;
            //    int firstHideRowIndex = firstDataRow + (headCount > 1 ? headCount : 2) * dtlRowsCount;
            //    for (int i = firstHideRowIndex; i < lastDataRowIndex - 1; i++)
            //    {
            //        sheet.GetRow(i).ZeroHeight = true;
            //    }
            //}

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
